package com.atguigu.mySmartCampus.Service.impl;

import com.atguigu.mySmartCampus.mapper.StudentMapper;
import com.atguigu.mySmartCampus.Service.StudentService;
import com.atguigu.mySmartCampus.entity.LoginForm;
import com.atguigu.mySmartCampus.entity.Student;
import com.atguigu.mySmartCampus.util.MD5;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

//+
@Transactional
@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student> implements StudentService {
    @Override
    public Student login(LoginForm loginForm) {
        QueryWrapper<Student> qw = new QueryWrapper<>();
        qw.eq("name", loginForm.getUsername());
        qw.eq("password", MD5.encrypt(loginForm.getPassword()));
        //..baseMapper.selectPage(qw);
        Student student = baseMapper.selectOne(qw);
        return student;
    }

    @Override
    public Student getStudentById(Long id) {
        QueryWrapper<Student> qw = new QueryWrapper<>();
        qw.eq("id", id);
        Student student = baseMapper.selectOne(qw);
        return student;
    }


    @Override
    //..
    // public IPage<Student> getStudentsByOperator(Page<Student> page, String studentName) {
    public IPage<Student> getStudentsByOperator(Page<Student> page, Student student) {
        QueryWrapper<Student> qw = new QueryWrapper<>();
        //..
        // if (!StringUtils.isEmpty(studentName)) qw.eq("name",studentName);
        if (!StringUtils.isEmpty(student.getName())) {qw.like("name", student.getName());}
        if (!StringUtils.isEmpty(student.getClazzName())){ qw.like("clazz_name", student.getClazzName());}
        QueryWrapper<Student> id = qw.orderByDesc("id");
        System.out.println("StudentServiceImpl.getStudentsByOperator"
        +"\n-->page size=" +page.getSize()
        +"\n-->page no=" +page.getCurrent()
        +"\n-->student=" +student
        );
        Page<Student> studentPage = baseMapper.selectPage(page, qw);
        return studentPage;
    }
}
